var base = getApp();
var _list = [];
Page({
  data: {
    loaded: false,
    mylist: [],
    pagenum: 1,
    ostatus: 0,//订单状态 0为全部 1为代付款 2为待发货 3为待收货 4为已完成
    tab: 0,
    url: base.path.url,
    scrollTop: 0,//距离顶部距离
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var _this = this;
    this.initData();
    wx.setNavigationBarTitle({
      title: '我的订单列表'
    });
    _this.setData({
      ostatus: options.ostatus,
      tab: options.ostatus
    })
    this.orderList();
  },

  // 订单详情
  goOrderDetail: function (e){
    var oid = e.currentTarget.dataset.oid;
    var status = e.currentTarget.dataset.status;
    wx.redirectTo({
      url: '../order_detail/order_detail?oid=' + oid + '&status=' + status
    })
  },
  onShareAppMessage: function () {
    return {
      title: base.user.shareTitle,
      desc: base.user.shareDesc,
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  // 订单列表
  orderList: function (){
    var that = this;
    wx.request({
      url: base.path.api + 'Member.GetUserOrders',
      data: { 
        md5key: base.user.session_key,
        aid: base.user.aid,
        ostatus: that.data.ostatus
      },
      method: "POST",
      header: base.user.header,
      success: function (response) {
        var result = response.data;
        var detail="";
        var mylist=[];
        if (result.ret == 200) {
          for(var i=0;i<result.data.length;i++){
            var json = result.data[i];
            var valname="";
            for (var j = 0; j < json.val_name.length;j++){
              valname += json.val_name[j].attr_name + "："+json.val_name[j].val_name + " "
            }
            json.detail = json.goods_name + "," + valname;
            mylist.push(json);
          }
          that.setData({
            mylist: mylist,
          })
            
        }
      }
    })
  },
  
  // 返回首页
  backindex:function(){
    wx.reLaunch({
      url: '../../index/index',
    })
  },
  // 确认收货
  bindGoodsOver: function (e) {
    var oid = e.currentTarget.dataset.oid;
    wx.showModal({
      content: '是否确认收货',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: base.path.api + 'Order.ConfirmOrder',
            data: {
              md5key: base.user.session_key,
              order_id: oid
            },
            method: "POST",
            header: base.user.header,
            success: function (response) {
              var result = response.data;
              if (result.ret == 200) {
                wx.showModal({
                  showCancel: false,
                  content: '确认收货成功',
                  success: function (res) {
                    if (res.confirm) {
                      wx.redirectTo({
                        url: '/pages/user/myorder/myorder?ostatus=' + 4,
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.cancel) {
          
        }
      }
    })
    
  },
  //配货中提示
  bindTips: function(){
    wx.showToast({
      title: '商品配货中，请耐心等待哦',
    })
  },
  initData: function () {
    this.getOrderList();  
  },
  bindCs: function(){

  },
  changeTab: function(e){
    var that=this;
    that.setData({
      ostatus: parseInt(e.currentTarget.dataset.index),
      tab: e.currentTarget.dataset.index,
      scrollTop: 0
    });
    this.orderList();
  },
  getOrderList: function () {
    var _this = this;

  },


  //支付
  bindPay: function(e){
    var orderno = e.currentTarget.dataset.orderno;
    var detail = e.currentTarget.dataset.detail;
    base.getPay(orderno, detail);
  },
  onReachBottom: function () {
    var _this = this;
    this.setData({
      "pagenum": this.data.pagenum + 1
    });
    this.getOrderList();
  },
  // 评价
  goComment: function (e) {
    var that = this;
    var gid = e.currentTarget.dataset.gid;
    var oid = e.currentTarget.dataset.oid;
    wx.navigateTo({
      url: '../comment/comment?gid=' + gid + '&oid=' + oid,
    })
  },
  // 查看物流
  goLogistics: function (e) {
    var that = this;
    var orderno = e.currentTarget.dataset.orderno;
    var skuimg = e.currentTarget.dataset.skuimg;
    wx.navigateTo({
      url: '../logistics/logistics?orderno=' + orderno + '&skuimg=' + skuimg,
    })
  },
})